package xyz.syyrjx.blog.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.reactive.function.client.WebClient;
import xyz.syyrjx.blog.entity.ai.AiDialogue;
import xyz.syyrjx.blog.exception.AiException;

import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2024/2/4 18:57
 * @description 常量池
 * @since v0.0.1
 */
public class ConstValuePool {

    // cookie生命
    public static final int COOKIE_MAX_AGE = 24 * 60 * 60;
    public static final int COOKIE_MAX_AGE_MILLIS = ConstValuePool.COOKIE_MAX_AGE * 1000;
    // cookie即将过期范围
    public static final int COOKIE_ABOUT_TO_EXPIRE_LIMIT = 1000 * 60 * 60 * 12;

    // ai使用次数
    public static final String QWEN_USAGE_COUNT = "QWEN_USAGE_COUNT";
    public static final String WANX_USAGE_COUNT = "WANX_USAGE_COUNT";
    public static final String CHAT_USAGE_COUNT = "CHAT_USAGE_COUNT";
    public static final String GPT4_USAGE_COUNT = "GPT4_USAGE_COUNT";
    public static final String AI_IDENTITY_FLAG = "AI_IDENTITY_FLAG";


    // ai模型
    public static final String QWEN_MODEL = "qwen";
    public static final String WANX_MODEL = "wanx";
    public static final String CHATGPT_MODEL = "chatgpt";
    public static final String GPT4_MODEL = "gpt4";

    // 通义千问
    public static final int QWEN_MAX_MESSAGE = 10;
    // 对话列表
    public static final String QWEN_DIALOGUES = "QWEN_DIALOGUES";
    public static final String WANX_DIALOGUES = "WANX_DIALOGUES";


    // 对话列表
    public static final String CHAT_DIALOGUES = "CHAT_DIALOGUES";
    public static final int CHAT_MAX_MESSAGE = 6;

    // 对话列表
    public static final String GPT4_DIALOGUES = "GPT4_DIALOGUES";
    public static final int GPT4_MAX_MESSAGE = 6;

    // 空列表
    public static final List<String> EMPTY_STRING_LIST = new ArrayList<>();
    public static final List<AiDialogue> EMPTY_AI_DIALOGUE_LIST = new ArrayList<>();

    // json解析对象
    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


    // openai代理客户端
    public static Queue<WebClient> PROXY_OPENAI_CLIENT_FREE = new LinkedList<>();
    public static Queue<WebClient> PROXY_OPENAI_CLIENT_BUSY = new LinkedList<>();
    public static int MAX_NUMBER_OF_PROXY_OPENAI_CLIENT_FREE = 10;

    private static final Lock openAiClientLock = new ReentrantLock();
    public static WebClient getAProxyOpenaiClient() {
        if (openAiClientLock.tryLock()) {
            try {
                if (PROXY_OPENAI_CLIENT_FREE.isEmpty()) {
                    throw new AiException("openai接口繁忙，请稍后再试");
                }
                WebClient webClient = PROXY_OPENAI_CLIENT_FREE.poll();
                PROXY_OPENAI_CLIENT_BUSY.add(webClient);
                return webClient;
            }finally {
                openAiClientLock.unlock();
            }
        }
        throw new AiException("openai接口繁忙，请稍后再试");
    }

    public static void releaseAProxyOpenaiClient(WebClient webClient) {
        PROXY_OPENAI_CLIENT_BUSY.remove(webClient);
        PROXY_OPENAI_CLIENT_FREE.add(webClient);
    }

    // markdown代码块标识
    public static final String MARKDOWN_CODE_BLOCK_START = "```";
    // markdown代码块结束标识
    public static final String MARKDOWN_CODE_BLOCK_END = "\n```";

    // 不想出现的内容描述
    public static final String IMAGE_NEGATIVE_PROMPT = "低分辨率、错误、最差质量、低质量、jpeg 伪影、丑陋、重复、病态、残缺、超出框架、多余的手指、变异的手、画得不好的手、画得不好的脸、突变、变形、模糊、脱水、不良的解剖结构、 比例不良、多余肢体、克隆脸、毁容、总体比例、畸形肢体、缺臂、缺腿、多余手臂、多余腿、融合手指、手指过多、长脖子、用户名、水印、签名";


    public static String BLOG_HOST;

}
